<?php
namespace Admin\Controller;
use Think\Controller;
class StatisticsController extends Controller {
    protected function _initialize() {
        $system['name']="交费系统";
        $this->assign('system',$system);
    }

    public function Statistics( $class_id = '', $update_timeStart = '',$update_timeEnd = '',$term_name="") {
        $this->assign('modelIcon', 'icon-user');
        $crumbsMap['Statistics'] = '信息'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '统计');
        $header = array(
            'sup'=>array(
                'name'=>'统计管理',
                'statistics'=>'active',
            ),
            'sub'=>array(
                'name'=>'统计列表',
                'statistics'=>'active',
            ),
            'icon'=>'icon-star',
            'url'=>'Statistics',
        );
        $this->assign('header', $header);
        //正常学生的id
        unset($map);
        $zhengchangId=[];
        $map['state'] = array(array('neq','退学'),array('neq','转学'),'and');
        $studentmodel=D('student')->where($map)->select();
        foreach($studentmodel as $k=>$v ) {
            $zhengchangId[]=$v['id'];
        }
        //退学和转学学生的id
        $zhuanxueId=[];
        $map['state']=array(array('eq','退学'),array('eq','转学'),'or');
        $tuistudent=D('student')->where($map)->select();
        foreach($tuistudent as $k=>$v ) {
            $zhuanxueId[]=$v['id'];
        }
        //查询功能
        $data=[];
        if($class_id != ""){
            $findMap=[];
            $student=D('student')->where(['class_id'=>$class_id])->select();
            foreach($student as $k=>$v){
                $findMap[]=$v['id'];
            }
            if($findMap){
            $data['student_id']=['IN',$findMap];
            }
        }
        //按时间筛选
        if($update_timeStart !='' && $update_timeEnd !=''){
            $data['update_time'] = ['between', [strtotime($update_timeStart), strtotime($update_timeEnd)]];
        }
        //按学期及交费项筛选
        if($term_name != ""){
            $terms=D('Term')->where(['name'=>['LIKE', '%'.$term_name.'%']])->select();
            $data=[];
            foreach($terms as $term){
                $tuition=D('Tuition')->where(['term_id'=>$term['id']])->select();
                foreach ($tuition as $value){
                    $data[]=$value['id'];
                }
            }
            if($data){
                $data['tuition_id']=['IN',$data];
            }
        }
        if($findMap){
            $arr=array_intersect($findMap,$zhengchangId);
            $arrs=array_intersect($findMap,$zhuanxueId);
        } else{
            $arr= $zhengchangId;
            $arrs=$zhuanxueId;
        }
        //正常学生应交的学费
        $wherea=$data;
        if(count($arr)!=""){
            $wherea['student_id']=['in',$arr]; 
            $feeModel=D('fee')->where($wherea)->select();
            $should=0;
            foreach($feeModel as $k=>$v){
                $tuition=D('Tuition')->where(['id'=>$v['tuition_id']])->find();
                $should=$should+$tuition['money'];

            }
        }else{
            $should=0;
        }
        //退学学生应交的学费
        $whereb=$data;
        if(count($arrs)!=""){
            $whereb['student_id']=['in',$arrs]; 
            $shouldSumb=D('fee')->field('sum(over-quit) as sum')->where($whereb)->select();
            $shouldSumb=$shouldSumb[0]['sum'];
        }else{
            $shouldSumb=0;
        }
        //应交总金额  
        $shouldSum=$should+$shouldSumb;
        $this->assign('shouldSum',$shouldSum);
        //总人数
        $model=D('fee')->where($data)->group('student_id')->select();
        $student=count($model);
        $this->assign('student',$student);
        //已缴费人数
        $data['over']=array('neq',0);
        $models=D('fee')->where($data)->group('student_id')->select();
        $newModel=count($models);
        $this->assign('newModel',$newModel);
        //已交总金额
        $overSum=D('fee')->field('sum(over-quit) as sum')->where($data)->select();
        $overSum = $overSum[0]['sum'];
        $this->assign('overSum', $overSum);
        //已腿总金额
        $quitSum=D('fee')->field('sum(quit) as sum')->where($data)->select();
        $quitSum = $quitSum[0]['sum'];
        $this->assign('quitSum', $quitSum);
        //已交清人数
         $result=D('fee')->where($data)->select();
            $results=[];
            foreach($result as $key=>$value) {
                $results[$value['student_id']][]= $value;
            }
            $num=0;
            foreach($results as $key=>$value){
                $student=D('student')->where(['id'=>$key])->find();
                $class=D('class')->where(['id'=>$student['class_id']])->find();
                $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
                $major=D('major')->where(['id'=>$class['major_id']])->find();
                //找到这个学生的交费项
                $tuition=D('tuition')->where(['grade_id'=>$grade['id'],'major_id'=>$major['id']])->select();
                $should=0;
                $over=0;
                foreach ($tuition as $k=>$v) {
                    $fees=D('fee')->where(['student_id'=>$key,'tuition_id'=>$v['id']])->find();
                    $should=$should+$v['money'];
                    $over=$over+$fees['over'];
                }
                if($should==$over){
                   $num=$num+1;
                }
            }

        $this->assign('num',$num);
        $fee = M('fee')->select();
        $this->assign('fee', $fee);
        $class = M('Class')->select();
        $this->assign('class', $class);
        $this->display();
    }
    
       
}